package org.dromara.oce.config;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import jakarta.annotation.PostConstruct;
import lombok.Data;
import org.dromara.common.core.constant.CacheNames;
import org.dromara.common.core.service.ConfigService;
import org.dromara.common.redis.utils.CacheUtils;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.system.domain.bo.SysConfigBo;
import org.dromara.system.domain.vo.SysConfigVo;
import org.dromara.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Data
@Component
@Configuration
@ConfigurationProperties("juliang")
public class JuLiangConfig {

    /**
     * 巨量接口地址
     */
    @Value("${juliang.url:https://ad.oceanengine.com}")
    private String url = "https://ad.oceanengine.com";

    /**
     * 广告主接口地址
     */
    private String advertiserUrl;

    public String getAdvertiserUrl() {
        return this.url + "/open_api/2";
    }

    /**
     * 巨量应用列表
     */
    private List<JuLiangProperties> juLiangConfigList;


    /**
     * 获取系统参数
     * @param paramKey
     * @return
     */
    public String getSystemParam(String paramKey){
        String key = "000000:sys_config";
        Map<String, String> map = RedisUtils.getCacheMap(key);
        if (CollectionUtils.isNotEmpty(map)) {
            return String.valueOf(map.getOrDefault(paramKey, null));
        }
        return null;
    }

//    @Autowired
//    private ISysConfigService sysConfigService;
    /**
     * 缓存系统参数
     */
//    @PostConstruct
//    public void cacheParams(){
//        List<SysConfigVo> configList = sysConfigService.selectConfigList(new SysConfigBo());
//        CacheUtils.clear(CacheNames.SYS_CONFIG);
//        for (SysConfigVo sysConfigVo : configList) {
//            CacheUtils.put("global:"+CacheNames.SYS_CONFIG, sysConfigVo.getConfigKey(),sysConfigVo.getConfigValue());
//        }
//    }

}
